from datetime import datetime
from .database import db
from .base import BaseModel

class Technician(BaseModel):
    __tablename__ = 'technicians'

    name = db.Column(db.String(50), nullable=False, comment='姓名')
    phone = db.Column(db.String(20), nullable=False, unique=True, comment='电话')
    id_card = db.Column(db.String(18), unique=True, comment='身份证号（唯一）')
    gender = db.Column(db.Integer, default=0, comment='性别（0-未知，1-男，2-女）')
    age = db.Column(db.Integer, comment='年龄')
    work_years = db.Column(db.Integer, comment='工龄（年）')
    head = db.Column(db.String(255), comment='头像URL')
    password = db.Column(db.String(255), nullable=False, comment='密码（哈希存储）')
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='技师ID')
    created_at = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    is_deleted = db.Column(db.Boolean, default=False, comment='是否删除（0-否，1-是）')

    # 添加索引
    __table_args__ = (
        db.Index('idx_name', 'name'),
    )

    def __repr__(self):
        return f'<Technician {self.id}: {self.name}>'

    def to_dict(self):
        result = super().to_dict()
        # 移除is_deleted字段
        result.pop('is_deleted', None)
        return result